<?php
defined('IN_MAIN') or exit('Invalid Access!');

function verify_form_data($dbname, $data){
global $vars;

foreach($data as $field=>$value){
 if(substr($field, 0, 1)=="_"){ 
  $field_name=substr($field, 1);
  if($vars["dbr"][$dbname][$field_name]){
   $check_data_type=false;
   $type=explode("#", $vars["dbr"][$dbname][$field_name]);
   if($type[3]=="m"){//type[3]=mandatory/optional (m/o)
    if(!strlen($value)){
     $errmsg.=replace_tag(__("'<%field%>' is a required field.")."<br />\n", array("<%field%>"=>__($type[4])));
    }else{
     $check_data_type=true;
    }
   }elseif(strlen($value)){
    $check_data_type=true;
   }
   if($check_data_type){
    if($type[0]=="str"){
     if(strlen($value)<$type[1] || strlen($value)>$type[2]){//type[1]=min value, type[2]=max value, type[4]=label for field
      $errmsg.=replace_tag(__("'<%field%>' must between <%min%> to <%max%> characters only. You have entered <%no%> character(s).")."<br />\n", array("<%field%>"=>__($type[4]), "<%min%>"=>$type[1], "<%max%>"=>$type[2], "<%no%>"=>strval(strlen($value))));
     }
    }elseif($type[0]=="dec"){
    //decimal - type[0]=decimal type[1]=max digit type[2]=decimal point type[3]=mandatory/optional type[4]=field label type[4]=field desc in english type[5]=min value type[6]=max value
     if(!is_numeric($value)){
      $errmsg.=replace_tag(__("'<%field%>' accepts only numerical value.")."<br />\n", array("<%field%>"=>__($type[4])));
     }elseif(strlen(number_format($value, $type[2], "", ""))>$type[1]){//check total length
      $errmsg.=replace_tag(__("'<%field%>' value is too large, maximum acceptable is <%no%>.")."<br />\n", array("<%field%>"=>__($type[4]), "<%no%>"=>number_format(pow(10, (($pow=$type[1]-$type[2])))-1).($type[2]>0? ".".(pow(10, $type[2])-1) : "")));
     }else{
      if(substr($type[5], 0, 2)!="na"){//check min
       if(substr($type[5], 0, 2)==">=" && doubleval($value)<doubleval(substr($type[5], 2))){
        $errmsg.=replace_tag(__("'<%field%>' must greater than or equal to <%no%>.")."<br />\n", array("<%field%>"=>__($type[4]), "<%no%>"=>number_format(substr($type[5], 2), $type[2])));
       }elseif(substr($type[5], 0, 1)==">" && substr($type[5], 0, 2)!=">=" && doubleval($value)<=doubleval(substr($type[5], 1))){
        $errmsg.=replace_tag(__("'<%field%>' must greater than <%no%>.")."<br />\n", array("<%field%>"=>__($type[4]), "<%no%>"=>number_format(substr($type[5], 1), $type[2])));
       }elseif(is_numeric($type[5]) && doubleval($value)<doubleval($type[5])){
        $errmsg.=replace_tag(__("'<%field%>' must greater than or equal to <%no%>.")."<br />\n", array("<%field%>"=>__($type[4]), "<%no%>"=>number_format($type[5], $type[2])));
       }
      }
      if(substr($type[6], 0, 2)!="na"){//check max
       if(substr($type[6], 0, 2)=="<=" && doubleval($value)>doubleval(substr($type[6], 2))){
        $errmsg.=replace_tag(__("'<%field%>' must less than or equal to <%no%>.")."<br />\n", array("<%field%>"=>__($type[4]), "<%no%>"=>number_format(substr($type[6], 2), $type[2])));
       }elseif(substr($type[6], 0, 1)=="<" && substr($type[6], 0, 2)!="<=" && doubleval($value)>=doubleval(substr($type[6], 1))){
        $errmsg.=replace_tag(__("'<%field%>' must less than <%no%>.")."<br />\n", array("<%field%>"=>__($type[4]), "<%no%>"=>number_format(substr($type[6], 1), $type[2])));
       }elseif(is_numeric($type[6]) && doubleval($value)>doubleval($type[6])){
        $errmsg.=replace_tag(__("'<%field%>' must less than or equal to <%no%>.")."<br />\n", array("<%field%>"=>__($type[4]), "<%no%>"=>number_format($type[6], $type[2])));
       }
      }
     }
    }
   }
  }
 }
}
return $errmsg;
}
?>